unit clsDevAssistencia;

interface

uses Dm, dbtables, biblio;

type
  TDevAssistencia = class(TObject)
  private
    Ent_Codigo: string;
    Dev_Data: TDateTime;
    Pro_Codigo: string;
  public
    Dev_Qtde: Double;
    Dev_Obs: string;
    Ass_Numero: string;
    function Gravar(p_Ent_Codigo, p_Pro_Codigo: string; p_Dev_Data: TDateTime): Byte;
    function Recuperar(p_Ent_Codigo, p_Pro_Codigo: string; p_Dev_Data: TDateTime): Byte;
    function Excluir(p_Ent_Codigo, p_Pro_Codigo: string; p_Dev_Data: TDateTime): Byte;
    function ExcluirTudo(p_Ent_Codigo: string; p_Dev_Data: TDateTime): Byte;
    procedure Consultar(var p_q: TQuery; p_Ent_Codigo: string; p_Dev_Data: TDateTime);
    function RecuperaQtde(p_Ent_Codigo, p_Pro_Codigo: string; p_Dev_Data: TDateTime): Double;
  end;

implementation

uses DB;

{ TDevAssistencia }

procedure TDevAssistencia.Consultar;
begin
  p_q.active := False;
  p_q.sql.clear;
  p_q.params.clear;
  p_q.sql.add('Select * from Dev_Assistencia where Ent_Codigo =:Ent and Dev_Data =:Dt');
  p_q.ParamByName('Ent').AsString := p_Ent_Codigo;
  p_q.ParamByName('Dt').AsDateTime := p_Dev_Data;
  p_q.Open;
end;

function TDevAssistencia.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Delete from Dev_Assistencia where Ent_Codigo =:Ent ' +
    'and Dev_Data =:Dt and Pro_Codigo =:Prod');
  tq.ParamByName('Ent').AsString := p_Ent_Codigo;
  tq.ParamByName('Prod').AsString := p_Pro_Codigo;
  tq.ParamByName('Dt').AsDateTime := p_Dev_Data;
  tq.Close;
  tq.ExecSQL;
  Result := 1;
  tq.Free;
end;

function TDevAssistencia.ExcluirTudo;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Delete from Dev_Assistencia where Ent_Codigo =:Ent ' +
    'and Dev_Data =:Dt');
  tq.ParamByName('Ent').AsString := p_Ent_Codigo;
  tq.ParamByName('Dt').AsDateTime := p_Dev_Data;
  tq.Close;
  tq.ExecSQL;
  Result := 1;
  tq.Free;
end;

function TDevAssistencia.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Dev_Assistencia where Ent_Codigo =:Ent ' +
    'and Dev_Data =:Dt and Pro_Codigo =:Prod');
  tq.ParamByName('Ent').AsString := p_Ent_Codigo;
  tq.ParamByName('Prod').AsString := p_Pro_Codigo;
  tq.ParamByName('Dt').AsDateTime := p_Dev_Data;
  tq.Open;
  if tq.Eof then begin
    tq.active := False;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Insert into Dev_Assistencia values (:Ent,:Dt,:Prod,:Qtde,:Obs');
    if Ass_Numero <> '' then
      tq.sql.add(',:Ass)')
    else
      tq.sql.add(',null)');
  end else begin
    tq.active := False;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update Dev_Assistencia set Dev_Qtde =:Qtde, Dev_Obs =:Obs');
    if Ass_Numero <> '' then
      tq.sql.add(', Ass_Numero=:Ass ')
    else
      tq.sql.add(', Ass_Numero=null ');
    tq.sql.add('where Ent_Codigo =:Ent and Dev_Data =:Dt and Pro_Codigo =:Prod');
  end;
  tq.ParamByName('Ent').AsString := p_Ent_Codigo;
  tq.ParamByName('Prod').AsString := p_Pro_Codigo;
  tq.ParamByName('Dt').AsDateTime := p_Dev_Data;
  tq.ParamByName('Qtde').AsFloat := Dev_Qtde;
  tq.ParamByName('Obs').AsString := Dev_Obs;
  if Ass_Numero <> '' then
    tq.ParamByName('Ass').AsString := Ass_Numero;
  tq.Close;
  tq.ExecSQL;
  Result := 1;
  tq.Free;
end;

function TDevAssistencia.RecuperaQtde(p_Ent_Codigo, p_Pro_Codigo: string;
  p_Dev_Data: TDateTime): Double;
var
  tq: TQuery;
begin
  inherited;
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('select sum(Dev_Qtde) Qtde ' +
    'from Dev_Assistencia ' +
    'where Ent_Codigo =:Ent ' +
    'and Dev_Data >=:Dt ' +
    'and Pro_Codigo =:Prod ' +
    'group by pro_Codigo');
  tq.ParamByName('Ent').AsString := p_Ent_Codigo;
  tq.ParamByName('Dt').AsDateTime := p_Dev_Data;
  ;
  tq.ParamByName('Prod').AsString := p_Pro_Codigo;
  tq.Open;
  if tq.Eof then
    Result := 0
  else
    Result := tq.FieldByName('Qtde').AsFloat;
  tq.Close;
  tq.Free;
end;

function TDevAssistencia.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Dev_Assistencia where Ent_Codigo =:Ent ' +
    'and Dev_Data =:Dt and Pro_Codigo =:Prod');
  tq.ParamByName('Ent').AsString := p_Ent_Codigo;
  tq.ParamByName('Prod').AsString := p_Pro_Codigo;
  tq.ParamByName('Dt').AsDateTime := p_Dev_Data;
  tq.Open;
  if tq.Eof then begin
    Result := 0;
    Dev_Qtde := 0;
    Dev_Obs := '';
    Ass_Numero := '';
  end else begin
    Result := 1;
    Dev_Qtde := tq.FieldByName('Dev_Qtde').AsFloat;
    Dev_Obs := tq.FieldByName('Dev_Obs').AsString;
    Ass_Numero := tq.FieldByName('Ass_Numero').AsString;
  end;
  tq.Close;
  tq.Free;
end;

end.
